﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class GioHang : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            GridView1.DataSource = (DataTable)Session["GioHang"];
            GridView1.DataBind();
            if (Session["TongTien"] != null)
            {
                AnBtnThangBao.Visible = true;
                lblTongCong.Text = "Total: " + Session["TongTien"].ToString() + "VNĐ";
            }
            else
            {
                AnBtnThangBao.Visible = false;
            }
           
        }
      
        dathang.Visible = false;
        
        
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            if (Session["GioHang"] != null)
            {
                int i = e.RowIndex;
                DataTable dt = new DataTable();
                Label lblSL = (Label)this.Master.FindControl("lblSL");
                Label lblTT = (Label)this.Master.FindControl("lblTongTien");
                dt = (DataTable)Session["GioHang"];
                lblSL.Text = (Convert.ToInt32(Session["TongSl"]) - Convert.ToInt32(dt.Rows[i][2])).ToString();
                lblTT.Text = (Convert.ToInt32(Session["TongTien"]) - Convert.ToInt32(dt.Rows[i][4])).ToString();
                dt.Rows[i].Delete();
                Session["GioHang"] = dt;
                Session["TongSL"] = lblSL.Text;
                Session["TongTien"] = lblTT.Text;
            }
            GridView1.DataSource = (DataTable)Session["GioHang"];
            GridView1.DataBind();
            lblTongCong.Text = "Total: " + Session["TongTien"].ToString() + "Vnđ";
        }
        catch (Exception ex)
        { 
        
        }
       
    }


    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = new DataTable();
            dt = (DataTable)Session["GioHang"];
            int tongSL = 0;
            int tongTien = 0;
            Label lblSL = (Label)this.Master.FindControl("lblSL");
            Label lblTT = (Label)this.Master.FindControl("lblTongTien");
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                TextBox txtSoLuong = (TextBox)GridView1.Rows[i].Cells[3].FindControl("txtSoLuong");
                dt.Rows[i][2] = int.Parse(txtSoLuong.Text);
                tongSL += int.Parse(txtSoLuong.Text);
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i][4] = Convert.ToInt32(dt.Rows[i][2]) * Convert.ToInt32(dt.Rows[i][3]);
                tongTien += Convert.ToInt32(dt.Rows[i][4]);
            }
            Session["GioHang"] = dt;
            Session["TongTien"] = tongTien;
            Session["TongSL"] = tongSL;
            lblSL.Text = Session["TongSL"].ToString();
            lblTT.Text = Session["TongTien"].ToString();
            GridView1.DataSource = (DataTable)Session["GioHang"];
            GridView1.DataBind();
            if (Session["TongTien"] != null)
                lblTongCong.Text = "Total: " + Session["TongTien"].ToString() + "Vnđ";
        }
        catch (Exception ex)
        {
            
        }
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        try
        {
            Session["GioHang"] = null;
            Session["TongTien"] = 0;
            Session["TongSL"] = 0;
            Label lblSL = (Label)this.Master.FindControl("lblSL");
            Label lblTT = (Label)this.Master.FindControl("lblTongTien");
            lblSL.Text = Session["TongSL"].ToString();
            lblTT.Text = Session["TongTien"].ToString();
            GridView1.DataSource = (DataTable)Session["GioHang"];
            GridView1.DataBind();
            if (Session["TongTien"] != null)
                lblTongCong.Text = "Total: " + Session["TongTien"].ToString() + "Vnđ";
        }
        catch (Exception ex)
        { 
        
        }
    }
    protected void btnDatHang_Click(object sender, EventArgs e)
    {
        if (Session["ID"] != null)
        {
            if (CheckBox1.Checked == true)
            {
                dhAccount.Text = Session["ID"].ToString();
                dhTongThanhTien.Text = Session["TongTien"].ToString()+ " Vnđ";
                GridGioHang.Visible = false;
                AnBtnThangBao.Visible = false;
                dathang.Visible = true;
                
            }
            else
            {
                ThongBao1.Text = "Bạn Chưa Đồng Ý Đặt Hàng";
            }
        }
        else
        {
            if (Session["Admin"] != null)
            {
                ThongBao1.Text = "Admin cũng mua hàng à";
            }
            else
            {
                ThongBao1.Text = " Chưa Đăng Nhập";
            }
        }
    }
    protected void GuiDangHang_Click(object sender, EventArgs e)
    {
        if (CheckBox2.Checked == true)
        {
            SanphamDTO DHGioHangSP = new SanphamDTO();
            UserDTO DHGioHangUS = new UserDTO();
            DHGioHangSP.Ngaydang = DateTime.Now.ToString();
            DHGioHangUS.ID1 = dhAccount.Text;
            DHGioHangUS.DiaChi1 = dhDIachigiaohang.Text;
            DHGioHangUS.SODT1 = dhSODTLL.Text;
            DHGioHangUS.Check1 = "waiting";
            DHGioHangSP.Gia1 = long.Parse(Session["TongTien"].ToString());
            SanPhamBUS.ThemGioHang(DHGioHangSP, DHGioHangUS);
            DHGioHangSP.MaGioHang1 = int.Parse(SanPhamBUS.LayMaGioHang().ToString());
            DataTable dt = (DataTable)Session["GioHang"];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DHGioHangSP.MaSP1 = dt.Rows[i][0].ToString();
                DHGioHangSP.Soluong1 = int.Parse(dt.Rows[i][2].ToString());
                SanPhamBUS.ThemChiTietGioHang(DHGioHangSP);

            }
            thongbao2.Text = " Đã Gữi Thành Công ! Chúng Tôi Sẽ Liên Hệ Với Bạn Trong Thời Gian Ngắn Nhất";
            GridGioHang.Visible = false;
            AnBtnThangBao.Visible = false;
            dathang.Visible = true;
            Session["GioHang"] = null;
            Session["TongTien"] = 0;
            Session["TongSL"] = 0;
        }
        else
        {
            GridGioHang.Visible = false;
            AnBtnThangBao.Visible = false;
            dathang.Visible = true;
            thongbao2.Text = " Hãy Kiểm Tra Lại ! Nếu Thông Tin Chính Xác Thì Hãy Check vào ô Kiểm Tra";
        }
    }
}
